// 防抖 一段时间awiat内，如果有新的触发再等待awiat时间，否则awiat结束触发

function debounce(fn,awiat){
    let timer = null
    return function f(){
        let context = this
        let args = arguments
        if(timer){
            clearTimeout(timer)
            timer = null 
        }
        timer = setTimeout(()=>{
            fn.apply(context,args)
        },awiat)
    }
}

//节流 一段时间内触发多次，只会执行最后一次

function throttle(fn,delay){
    let lasttime
    let delay = delay || 20
    let timer = null
    return function(){
        let args = arguments
        let that = this
        let now = Date.now()
        if(lasttime&&lasttime-now<delay){
            clearTimeout(timer)
            timer = setTimeout(() => {
                fn.apply(that,args)
            }, delay); 
        }
        else {
            lasttime = Date.now() 
            fn.apply(that,args)
        }
    }
}